
import { getInfo, getInfobyId } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    // 更新存储token
    updataToken(state, token) {
      state.token = token
      setToken(token)
    },
    // 删除token
    removeTokens(state) {
      state.token = ''
      // 删除本地token
      removeToken()
    },
    // 更新储存用户信息
    updatauserInfo(state, info) {
      state.userInfo = info
    },
    // 清空用户信息
    deluserinfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async getuserInfo(content) {
      // 获取用户信息
      try {
        const res = await getInfo()
        console.log(res, 'res-----获取资料成功')
        // 获取头像id
        const info = await getInfobyId(res.data.userId)
        console.log(info.data, 'info的信息')
        content.commit('updatauserInfo', { ...res.data, ...info.data })
      } catch (err) {
        console.log(err)
      }
    }
  }
}
